import 'package:flutter/material.dart';

class Test005 extends StatefulWidget {
  const Test005({Key? key}) : super(key: key);

  @override
  State<Test005> createState() => _Test005State();
}

class _Test005State extends State<Test005> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Animation<Offset> _getTween(double start, double end) {
    return Tween(begin: Offset.zero, end: const Offset(0.1, 0))
        .chain(
          CurveTween(curve: Interval(start, end)),
        )
        .chain(
          CurveTween(curve: Curves.bounceOut),
        )
        .animate(_controller);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("动画"),
      ),
      body: Center(
        child: Column(
          children: [
            const SizedBox(height: 20),
            SlideTransition(
              position: _getTween(0.0, 0.2),
              child: Container(width: 300, height: 100, color: Colors.blue[100]),
            ),
            SlideTransition(
              position: _getTween(0.2, 0.4),
              child: Container(width: 300, height: 100, color: Colors.blue[300]),
            ),
            SlideTransition(
              position: _getTween(0.4, 0.6),
              child: Container(width: 300, height: 100, color: Colors.blue[500]),
            ),
            SlideTransition(
              position: _getTween(0.6, 0.8),
              child: Container(width: 300, height: 100, color: Colors.blue[700]),
            ),
            SlideTransition(
              position: _getTween(0.8, 1.0),
              child: Container(width: 300, height: 100, color: Colors.blue[900]),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _controller.repeat(reverse: true);
        },
        child: const Icon(Icons.ads_click),
      ),
    );
  }
}
